import axios from "axios";
import Vue from "vue";

// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 10000, // request timeout
});

// request interceptor
service.interceptors.request.use(
  (config) => {


    let token = localStorage.getItem("token");
    if (token) {
      // let each request carry token
      // ['X-Token'] is a custom headers key
      // please modify it according to the actual situation
      config.headers["token"] = token
    }

    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  (response) => {
    const res = response.data;

    return res;
  },
  (error) => {
    // console.log('err' + error) // for debug
    // Message({
    //   message: error.message,
    //   type: 'error',
    //   duration: 5 * 1000
    // })

    // // do something with request error
    // console.log(error) // for debug

    if (error.response) {
      const data = error.response.data;
      let token = localStorage.getItem("token");
      console.log("------异常响应------", token);
      console.log("------异常响应------", error.response.status);
      switch (error.response.status) {
        case 403:
          Vue.prototype.$notify.error({
            title: "系统提示",
            message: "拒绝访问",
          });
          break;
        case 500:
          console.log("------error.response------", error.response);
          if (token && data.message.includes("Token失效")) {
            // update-begin- --- author:scott ------ date:20190225 ---- for:Token失效采用弹框模式，不直接跳转----
            if (/wxwork|dingtalk/i.test(navigator.userAgent)) {
              Vue.prototype.$message.error({
                message: "登录已过期，正在重新登录",
              });
            } else {
              Vue.prototype
                .$confirm("很抱歉，登录已过期，请重新登录", "登陆已过期", {
                  confirmButtonText: "重新登录",
                  type: "error",
                })
                .then((res) => {

                });
            }
            // update-end- --- author:scott ------ date:20190225 ---- for:Token失效采用弹框模式，不直接跳转----
          }
          break;
        case 404:
          Vue.prototype.$notify.error({
            title: "系统提示",
            message: "很抱歉，资源未找到!",
          });
          break;
        case 504:
          Vue.prototype.$notify.error({
            title: "系统提示",
            message: "网络超时",
          });
          break;
        case 401:
          Vue.prototype.$notify.error({
            title: "系统提示",
            message: "很抱歉，登录已过期，请重新登录",
          });
          break;
        default:
          Vue.prototype.$notify.error({
            title: "系统提示",
            message: data.message,
          });
          break;
      }
    } else if (error.message) {
      if (error.message.includes("timeout")) {
        Vue.prototype.$notify.error({ title: "系统提示", message: "网络超时" });
      } else {
        Vue.prototype.$notify.error({
          title: "系统提示",
          message: error.message,
        });
      }
    }
    return Promise.reject(error);
  }
);


export default service;
